﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Namespace Fonts

    Public Class MirFont

        Dim mSystemNormalFont As Drawing.Font
        Dim mSystemBoldFont As Drawing.Font

        Dim mD3DNormalFont As Direct3D.Font
        Dim mD3DBoldFont As Direct3D.Font

        'Dim textTexture(99) As Direct3D.Texture
        'Dim textSurface(99) As Direct3D.Surface
        Dim textTexture As Direct3D.Texture
        Dim textSurface As Direct3D.Surface
        Dim rhlper As Direct3D.RenderToSurface

        Public Sub New(ByRef dev As Direct3D.Device, ByRef fontname As String, ByRef fontsize As Integer)

            mSystemNormalFont = New Drawing.Font(fontname, fontsize, FontStyle.Regular)
            mSystemBoldFont = New Drawing.Font(fontname, fontsize, FontStyle.Bold)

            mD3DNormalFont = New Direct3D.Font(dev, mSystemNormalFont)
            mD3DBoldFont = New Direct3D.Font(dev, mSystemBoldFont)

            rhlper = New Direct3D.RenderToSurface(DirectX.D3D, DirectX.DispMode.Width, DirectX.DispMode.Height, Direct3D.Format.A8R8G8B8, True, Direct3D.DepthFormat.D16)

            'For i As Integer = 0 To 99
            'textTexture(i) = New Direct3D.Texture(DirectX.D3D, DirectX.DispMode.Width, DirectX.DispMode.Height, 1, Direct3D.Usage.RenderTarget, Direct3D.Format.A8R8G8B8, Direct3D.Pool.Default)
            'textSurface(i) = textTexture(i).GetSurfaceLevel(0)
            'Next

            textTexture = New Direct3D.Texture(DirectX.D3D, DirectX.DispMode.Width, DirectX.DispMode.Height, 1, Direct3D.Usage.RenderTarget, Direct3D.Format.A8R8G8B8, Direct3D.Pool.Default)
            textSurface = textTexture.GetSurfaceLevel(0)

        End Sub

        Public Function GetNormalTextWidth(ByRef text As String) As Integer

            Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemNormalFont, New Size(DirectX.DispMode.Width, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak Or TextFormatFlags.NoPadding).Width

        End Function

        Public Function GetNormalTextHeight(ByRef text As String, Optional ByRef fixedwidth As Integer = 0) As Integer

            If fixedwidth > 0 Then
                Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemNormalFont, New Size(fixedwidth, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak).Height
            Else
                Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemNormalFont, New Size(DirectX.DispMode.Width, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak).Height
            End If

        End Function

        Public Function GetBoldTextWidth(ByRef text As String) As Integer

            Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemBoldFont, New Size(DirectX.DispMode.Width, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak).Width

        End Function

        Public Function GetBoldTextHeight(ByRef text As String, Optional ByRef fixedwidth As Integer = 0) As Integer

            If fixedwidth > 0 Then
                Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemBoldFont, New Size(fixedwidth, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak).Height
            Else
                Return System.Windows.Forms.TextRenderer.MeasureText(text, mSystemBoldFont, New Size(DirectX.DispMode.Width, DirectX.DispMode.Height), TextFormatFlags.Left Or TextFormatFlags.ExpandTabs Or TextFormatFlags.NoClipping Or TextFormatFlags.WordBreak).Height
            End If

        End Function

        Public Sub DrawNormalText(ByRef text As String, ByRef x As Integer, ByRef y As Integer, ByRef colr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)


            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0) ' Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()

            mD3DNormalFont.DrawText(Nothing, text, x, y, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawNormalText(ByRef text As String, ByRef destrect As Rectangle, ByRef fmt As Microsoft.DirectX.Direct3D.DrawTextFormat, ByRef colr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()

            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawBorderText(ByRef text As String, ByRef destrect As Rectangle, ByRef fmt As Microsoft.DirectX.Direct3D.DrawTextFormat, ByRef colr As Color, ByRef bdrcolr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()
            destrect.Offset(New Point(1, 0))
            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(-2, 0))
            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(1, 1))
            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(0, -2))
            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(0, 1))
            mD3DNormalFont.DrawText(Nothing, text, destrect, fmt, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)

            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)

            DirectX.GameSprite.End()
        End Sub

        Public Sub DrawBorderText(ByRef text As String, ByRef x As Integer, ByRef y As Integer, ByRef colr As Color, ByRef bdrcolr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()
            mD3DNormalFont.DrawText(Nothing, text, x - 1, y, bdrcolr)
            mD3DNormalFont.DrawText(Nothing, text, x + 1, y, bdrcolr)
            mD3DNormalFont.DrawText(Nothing, text, x, y - 1, bdrcolr)
            mD3DNormalFont.DrawText(Nothing, text, x, y + 1, bdrcolr)
            mD3DNormalFont.DrawText(Nothing, text, x, y, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawBoldBorderText(ByRef text As String, ByRef destrect As Rectangle, ByRef fmt As Microsoft.DirectX.Direct3D.DrawTextFormat, ByRef colr As Color, ByRef bdrcolr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()
            destrect.Offset(New Point(1, 0))
            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(-2, 0))
            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(1, 1))
            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(0, -2))
            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, bdrcolr)
            destrect.Offset(New Point(0, 1))
            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawBoldBorderText(ByRef text As String, ByRef x As Integer, ByRef y As Integer, ByRef colr As Color, ByRef bdrcolr As Color, ByRef z As Single)

            'Dim idx As Integer = GetNextSurf()

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()
            mD3DBoldFont.DrawText(Nothing, text, x - 1, y, bdrcolr)
            mD3DBoldFont.DrawText(Nothing, text, x + 1, y, bdrcolr)
            mD3DBoldFont.DrawText(Nothing, text, x, y - 1, bdrcolr)
            mD3DBoldFont.DrawText(Nothing, text, x, y + 1, bdrcolr)
            mD3DBoldFont.DrawText(Nothing, text, x, y, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawBoldText(ByRef text As String, ByRef x As Integer, ByRef y As Integer, ByRef colr As Color, ByRef z As Single)

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)


            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0) ' Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()

            mD3DBoldFont.DrawText(Nothing, text, x, y, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public Sub DrawBoldText(ByRef text As String, ByRef destrect As Rectangle, ByRef fmt As Microsoft.DirectX.Direct3D.DrawTextFormat, ByRef colr As Color, ByRef z As Single)

            DirectX.D3D.EndScene()
            'rhlper.BeginScene(textSurface(idx))
            rhlper.BeginScene(textSurface)

            'DirectX.D3D.SetRenderTarget(0, textSurface(idx))

            DirectX.D3D.Clear(Microsoft.DirectX.Direct3D.ClearFlags.Target Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0F, 0) 'Or Direct3D.ClearFlags.ZBuffer, Color.Transparent, 1.0, 0)
            'DirectX.D3D.BeginScene()

            mD3DBoldFont.DrawText(Nothing, text, destrect, fmt, colr)

            'DirectX.D3D.EndScene()

            rhlper.EndScene(Direct3D.Filter.None)

            'DirectX.D3D.SetRenderTarget(0, DirectX.GameSurface)

            DirectX.D3D.BeginScene()

            'DirectX.BlitD3D(textTexture(idx), 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.Begin(Direct3D.SpriteFlags.AlphaBlend)
            DirectX.BlitD3D(textTexture, 0, 0, DirectX.DispMode.Width, DirectX.DispMode.Height, z)
            DirectX.GameSprite.End()

        End Sub

        Public ReadOnly Property D3DNormalFont() As Direct3D.Font
            Get
                Return mD3DNormalFont
            End Get
        End Property

        'Public ReadOnly Property D3DBoldFont() As Direct3D.Font
        '    Get
        '        Return mD3DBoldFont
        '    End Get
        'End Property

        Public ReadOnly Property SystemNormalFont() As Drawing.Font
            Get
                Return mSystemNormalFont
            End Get
        End Property

        Public ReadOnly Property SystemBoldFont() As Drawing.Font
            Get
                Return mSystemBoldFont
            End Get
        End Property

        Public Sub Dispose()

            mD3DNormalFont.Dispose()
            mD3DBoldFont.Dispose()

        End Sub

    End Class

End Namespace
